package com.xscha.soyue.common.core.mapper;

import cn.hutool.core.collection.CollUtil;
import com.xscha.soyue.common.core.exception.SoyRuntimeException;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * BeanMapperService Bean拷贝映射
 *
 * @author Vondser
 * @version V2024.1.0
 */
public interface BeanMapperService {

	default <S, T> List<T> mapAsList(Collection<S> source, Class<T> clz) throws SoyRuntimeException {
		if (CollUtil.isEmpty(source)) {
			return Collections.emptyList();
		}
		return source.stream().map(s -> map(s, clz, false)).collect(Collectors.toList());
	}

	default <S, T> Set<T> mapAsSet(Collection<S> source, Class<T> clz) throws SoyRuntimeException {
		if (CollUtil.isEmpty(source)) {
			return Collections.emptySet();
		}
		return source.stream().map(s -> map(s, clz, false)).collect(Collectors.toSet());
	}

	<S, T> T map(S source, Class<T> clz, boolean cycles) throws SoyRuntimeException;

	<S, T> T map(S source, Type type, boolean cycles) throws SoyRuntimeException;

	<S, T> T map(S source, T target, boolean cycles) throws SoyRuntimeException;

	void registerConverter(Object converter);

}
